#include "docview.h"
#include <QDebug>
/*
 *
 * 显示pdf的小窗口
 *
 */

DocView::DocView(PDFKit *p, QWidget *parent):pdf(p), QWidget(parent), multiple(1)
{
    page = 0;
    y = 0;
    resize(pdf->get_pageWidth(), pdf->get_totalPage()*(pdf->pageSize.height())+10);
}


void DocView::paintEvent(QPaintEvent *)
{
#if 1
    QPainter painter(this);

    int x = this->width() - pdf->get_pageWidth();

    if(page == 0)
    {
        painter.drawImage(x/2, y, pdf->get_page_image(page));
        painter.drawImage(x/2, y+pdf->get_pageHeight()+10, pdf->get_page_image(page+1));
        painter.drawImage(x/2, y+pdf->get_pageHeight()*2+20, pdf->get_page_image(page+2));
        painter.drawImage(x/2, y+pdf->get_pageHeight()*3+30, pdf->get_page_image(page+3));
    }
    else if(page == pdf->totalPage-1)
    {
        painter.drawImage(x/2, y+10, pdf->get_page_image(page));
    }
    else
    {
        painter.drawImage(x/2, y-pdf->get_pageHeight(), pdf->get_page_image(page-1));
        painter.drawImage(x/2, y+10, pdf->get_page_image(page));
        painter.drawImage(x/2, y+pdf->get_pageHeight()+20, pdf->get_page_image(page+1));
        painter.drawImage(x/2, y+pdf->get_pageHeight()*2+30, pdf->get_page_image(page+2));
        painter.drawImage(x/2, y+pdf->get_pageHeight()*3+40, pdf->get_page_image(page+3));
    }
#else
    QPainter painter(this);
    int nScrollHeight = 0; //当前窗口的高度
    int nScrollYFlag = 0;  //当前坐标（画布）
qDebug() << "aaaa";
    DocWidget *p = static_cast<DocWidget *>(this->parentWidget());
    if(p == nullptr)
        return ;
qDebug() << "111";
qDebug() << p->objectName();
    QScrollArea *scroll = p->scrollArea;
    if(scroll == nullptr)
        return ;

    nScrollHeight = p->scrollArea->size().height(); //当前绘制的剩余窗口高度
    nScrollYFlag  = p->scrollArea->verticalScrollBar()->value();
    //首先刷一下成灰色
qDebug() << "2222";
    // 当前绘制的页码
    int nPageNum;
    // 当前页码绘制的起始VPos
    int nPageVPos;
    // 当前页码的图片
    QImage img;
    QImage imageCopy;

    int nXDrawPos = 0;

    while (nScrollHeight > 0)
    {
        GetVPos(nScrollYFlag, nPageNum, nPageVPos);
        // 绘制当前页的高度
        int nRenderHeight = pdf->get_pageHeight() + 8 - nPageVPos;

        imageCopy = pdf->get_page_image(nPageNum);
        img = imageCopy.copy(0, nPageVPos, pdf->get_pageWidth(), nRenderHeight);

        QSize docWidget = p->size();
        nXDrawPos = (docWidget.width() - pdf->get_pageWidth()) / 2;
        if (nXDrawPos < 0)
            nXDrawPos = p->scrollArea->verticalScrollBar()->size().width() / 2;

        painter.drawImage(nXDrawPos, nScrollYFlag, img);

        nScrollHeight -= img.height();
        nScrollYFlag += img.height();
    }
#endif
}

bool DocView::GetVPos(int nScrollPos, int &nPageNum, int &nPageVStartPos)
{
    int nPageCount = pdf->totalPage;

    for (int i = 0; i < nPageCount; i++)
    {
        if (nScrollPos < (pdf->get_pageHeight() + 8))
        {
            nPageNum = i;
            nPageVStartPos = nScrollPos;
            break;
        }
        nScrollPos -= (pdf->get_pageHeight() + 8);
    }
    nScrollPos += 8;

    return true;
}


void DocView::valueChange(int value)
{
    page = value / (pdf->get_pageHeight());
    y = (page) * (pdf->get_page_image(page).height());

    update();
}

void DocView::gotoPage(int page)
{

}

void DocView::changeImageSize(int flag)
{
    if(flag > 0)
    {
        if(multiple < 2)
            multiple += 0.25;
    }
    else if(flag < 0)
    {
        if(multiple > 0.25)
            multiple -= 0.25;
    }
    else
    {
        multiple = 1;
    }
    pdf->multiple = multiple;
//    update();
}

